﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using PaulBeck.Timesheet.BOL;

namespace PaulBeck.Timesheet.WebParts.Week
{
    public partial class WeekUserControl : UserControl
    {

        #region "Properties"

        public int UserId
        {
            get
            {
                object o = ViewState["UserId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["UserId"] = value;
            }
        }

        public int WeekId
        {
            get
            {
                object o = ViewState["WeekId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["WeekId"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void DisplayWeeks(int userId)
        {
            if (userId != UserId) { WeekId = 0; } // Change weekid  i.e. non set or consultant has changed 
            ddlWeek.Items.Clear();
            UserId = userId;
            string loginUserName = CommonWFE.CurrentUserName();
            ListItemCollection lic = BOL.Common.GetWeekCommencingDates(userId, DateTime.Now.AddDays((int)(DayOfWeek.Saturday - DateTime.Now.DayOfWeek)), loginUserName);
            int iSelected = 0;
            foreach (ListItem li in lic)
            {
                ddlWeek.Items.Add(li);
                if (li.Value == WeekId.ToString())
                {
                    ddlWeek.SelectedIndex = iSelected;
                }
                iSelected++;
            }
            if (WeekId == 0)
            {
                ddlWeek.SelectedIndex = 0;
                int tmpWeekId = Convert.ToInt32(ddlWeek.Items[0].Value);
                if (tmpWeekId > 0)
                {
                    WeekId = tmpWeekId;
                }
                else
                {
                    try
                    {
                        DateTime selDT = Convert.ToDateTime(ddlWeek.Items[0].Text.Replace(" (Unsubmitted)", ""));
                        WeekId = ConsultantBOL.GetWeekEntryIdIdByUserIdForDay(UserId, selDT);     // Get the week
                    }
                    catch (Exception ex)
                    {
                        //TODO! Logg error
                    }
                }
            }
        }

        protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e)
        {
            int valueSelected = Convert.ToInt32(ddlWeek.SelectedValue);
            if (valueSelected < 1)
            {
                DateTime selDT = Convert.ToDateTime(ddlWeek.SelectedItem.Text.Replace(" (Missing)", ""));
                WeekId = ConsultantBOL.GetWeekEntryIdIdByUserIdForDay(UserId, selDT);     // Get the week
            }
            else
            {
                WeekId = Convert.ToInt32(ddlWeek.SelectedValue);
            }
        }

        public int GetWeekId()
        {
            return WeekId;
        }

    }
}
